<include file="../header" />
<div class="wrapper">
    <div class="header text-center font-16">
        快捷报修
        <div class="iconfont icon-zuo go_index"></div>
    </div>
    <div class="history-list">
        <div class="addimg clearfix" id="repair_image">
            <div class="load-img pull-left">
                <div class="img">
                    <div class="load text-center">
                        +
                    </div>
                </div>
            </div>
        </div>
        <div class="repair-list describe-content">
            <input type="text" placeholder="请描述报修内容" id="content">
        </div>
        <div class="repair-list gray2">
            <select name="project_id" id="project_id">
                {BEESCRM:$project}
            </select>
            <div class="iconfont icon-zuo-copy "></div>
        </div>
        <div class="repair-list gray2">
            <select name="project_child_id" id="project_child_id">
                <option value="0">请选择二级报修项目</option>
            </select>
            <div class="iconfont icon-zuo-copy "></div>
        </div>
        <div class="repair-list gray2" id="user_address">
            <input id="address_id" type="hidden" value="{BEESCRM:$address['address_id']}">
            <input id="address_detail" type="hidden" value="{BEESCRM:$address['address_detail']}">
            <empty name="address">
                <span>请选择报修地址</span>
            <else/>
                <span>{BEESCRM:$address['community_name']}{BEESCRM:$address['building_name']}{BEESCRM:$address['floor_name']}{BEESCRM:$address['room_name']}</span>
            </empty>
            <div class="iconfont icon-you "></div>
        </div>
        <div class="repair-list clearfix">
            <p class="pull-right">
                预计费用： <span class="blue2" id="cost">0</span> 元
            </p>
        </div>
        <input type="hidden" id="serverId" value="">
        <input type="hidden" id="localIds" value="">
    </div>
    <div class="flex repair-btn">
        <a href="#" class="btn text-center btn-left" id="order_submit">提交</a>
        <a href="#" class="btn text-center btn-right gray-bg go_index">取消</a>
    </div>
</div>
<include file="../getphotos"/>
<script src="/assets/repaireast/js/layer.js"></script>
<script src="/assets/repaireast/js/public.js"></script>
<script src="/assets/repaireast/js/jquery.cookie.js"></script>
<script type="text/javascript" src="/assets/bower_components/upload/jquery.ui.widget.js"></script>
<script type="text/javascript" src="/assets/bower_components/upload/jquery.fileupload.js"></script>
<script type="text/javascript" src="/assets/bower_components/upload/jquery.iframe-transport.js"></script>
<script>
    $(function(){
        var serverId = '';
        $('.img').click(function(){
            wx.ready(function(){
                wx.chooseImage({
                    count: 1, // 默认9
                    sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
                    sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
                    success: function (res) {
                        var localIds = res.localIds;
                        $('#repair_image .img').html('<img src="'+ localIds[0] +'">');

                        $('#localIds').val(localIds[0]);
                        var localId = localIds.pop();
                        wx.uploadImage({
                            localId: localId, // 需要上传的图片的本地ID，由chooseImage接口获得
                            isShowProgressTips: 1, // 默认为1，显示进度提示
                            success: function (res) {
                                serverId = res.serverId; // 返回图片的服务器端ID
                                $('#serverId').val(serverId);
                            }
                        });
                    }
                });
            });
        })



        //报修图片
        if ('{BEESCRM:$cookie_image}'){       	
            var html = '<img src="'+'{BEESCRM:$cookie_image}'+'">';
            $('#repair_image .img').html(html);
        }
        //报修内容
        if ('{BEESCRM:$cookie_content}'){
            $('#content').val('{BEESCRM:$cookie_content}');
        }
        //报修项目
        if ('{BEESCRM:$cookie_project_id}'){
            $('#project_id').val('{BEESCRM:$cookie_project_id}');

            $.ajax({
                type:'post',
                url:"/Repaireast/RepaireastRepair/ajaxGetSecondProject",
                dataType:'json',
                data:{
                    'project_id' : '{BEESCRM:$cookie_project_id}'
                },
                success:function(data){
                    if(data.status=='S'){
                        option_html = '<option value="0">请选择二级报修项目</option>';
                        $.each(data.data, function(k,v){
                            if (v.id == '{BEESCRM:$cookie_project_child_id}'){
                                option_html += '<option value="' + v.id + '" data-cost="' + v.cost + '" selected>' + v.name + '</option>';
                            }else {
                                option_html += '<option value="' + v.id + '" data-cost="' + v.cost + '">' + v.name + '</option>';
                            }
                        });
                        $('#project_child_id').html(option_html);
                    }
                }
            });
        }

        //报修金额
        if ('{BEESCRM:$cookie_cost}'){
            $('#cost').text('{BEESCRM:$cookie_cost}');
        }
    });

   /* $('.img').click(function () {
        $('.file').click();
    });

    //图片上传
    $(".file").fileupload({
        url:"/Base/Common/upload/is_thumb/1",//文件上传地址，当然也可以直接写在input的data-url属性内
//        beforeSend:function(){
//            $('.load').show();
//        },
        done:function(e,result){
            //done方法就是上传完毕的回调函数，其他回调函数可以自行查看api
            //注意result要和jquery的ajax的data参数区分，这个对象包含了整个请求信息
            //返回的数据在result.result中，假设我们服务器返回了一个json对象
            var data=$.parseJSON(result.result);
            if(data.status=='S'){
                var html = '<img src="'+data.thumb_url+'">';
                $('#repair_image .img').html(html);
            }else{
                fm.alert(data.message);
                return false;
            }
        }
    });*/

    //二级项目
    $('#project_id').change(function(){
        var html = '<option value="0" data-cost="0">请选择二级报修项目</option>';
        if ($(this).val() > 0){
            $.ajax({
                type:'post',
                url:"/Repaireast/RepaireastRepair/ajaxGetSecondProject",
                dataType:'json',
                data:{
                    'project_id' : $(this).val()
                },
                success:function(data){
                    if(data.status=='S'){
                        $.each(data.data, function(k,v){
                            html += '<option value="'+ v.id +'" data-cost="'+ v.cost +'">'+ v.name +'</option>';
                        });
                        $('#project_child_id').html(html);
                    }
                }
            });
        }else{
            $('#project_child_id').html(html);
        }
    });

    $('#project_child_id').change(function(){
        var cost = $(this).find('option:selected').data('cost');
        $('#cost').text(cost);
    });

    $('#user_address').click(function(){
        var image = $('#localIds').val();
        var content = $('#content').val();
        var project_id = $('#project_id').val();
        var project_child_id = $('#project_child_id').val();
        var cost = $('#cost').text();
        var serverId = $('#serverId').val();

		$.ajax({
                type: 'post',
                url: "/Repaireast/RepaireastRepair/saveFastRepair",
                dataType: 'json',
                data: {
                    'image': image,
                    'content': content,
                    'project_id': project_id,
                    'project_child_id': project_child_id,
                    'cost': cost,
                    'serverId':serverId
                },
                success: function (data) {
                	if(data.status == 'S'){                		
                		window.location.href='/Repaireast/RepaireastRepair/userAddress/type/3';
                	}
                },
                error: function () {
                    fm.alert('通讯失败，请稍后再试');
                }
            });

    });
    
    //提交数据
    $('#order_submit').click(function(){
        var image = $('#repair_image img').attr('src');
        if (!image){
            fm.alert('请上传报修图片！');
            return false;
        }
        var content = $('#content').val();
        if (!content){
            fm.alert('请描述报修内容！');
            return false;
        }
        var project_id = $('#project_id').val();
        if (project_id == 0){
            fm.alert('请选择一级报修项目！');
            return false;
        }
        var project_child_id = $('#project_child_id').val();
        if (project_child_id == 0){
            fm.alert('请选择二级报修项目！');
            return false;
        }
        var address_id = $('#address_id').val();
        if (!address_id){
            fm.alert('请选择报修地址！');
            return false;
        }
        var address_detail = $('#address_detail').val();
        if (address_detail == ''){
            fm.alert('请完善您的报修地址（如园区、楼幢等）！');
            return false;
        }

        var cost = $('#cost').text();
        //var serverId = $('#serverId').val();
        var serverId = '{BEESCRM:$cookie_serverId}';

        $.ajax({
            type:'post',
            url:"/Repaireast/RepaireastRepair/doFastRepair",
            dataType:'json',
            data:{
                'image' : image,
                'content' : content,
                'project_id' : project_id,
                'project_name' : $('#project_id option:selected').text(),
                'project_child_id' : project_child_id,
                'project_child_name' : $('#project_child_id option:selected').text(),
                'address_id' : address_id,
                'cost' : cost,
                'serverId':serverId
            },
            success:function(data){
                fm.alert('操作成功!');
                setTimeout(function(){
                    window.location.href=data.url;
                },2000);
            },
            error:function(){
                fm.alert('通讯失败，请稍后再试');
            }
        });
    });
</script>
<include file="../footer" />